package pierwszy.wordPressPageObjects;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AllPostsPage {
	WebDriver driver;
	@FindBy (how=How.ID, using="the-list")
	WebElement postsContainer;
	@FindBy (how=How.ID, using="post-search-input")
	WebElement searchPosts;
	@FindBy (how=How.ID, using="cat")
	WebElement viewByCategories;
	@FindBy (how=How.XPATH, using="//*[@id='menu-posts']/a/div[@class='wp-menu-name']")
	WebElement posts;
	WebElement allPosts;
	WebElement addNewPost;
	WebDriverWait wait;
	
	public AllPostsPage (WebDriver driver) {
		this.driver = driver;
		wait = new WebDriverWait(driver,30);
		//driver.get("http://pageobjectpattern.wordpress.com/wp-admin/edit.php");
	}
	
	public void createANewPost(String title, String description){
		Actions builder = new Actions(driver);
		builder.moveToElement(posts).build().perform();
		
		wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='menu-posts']//a[contains(@href,'post-new.php')]")));
		addNewPost = driver.findElement(By.xpath("//*[@id='menu-posts']//a[contains(@href,'post-new.php')]"));
		
		addNewPost.click();
		AddNewPostPage newPost = PageFactory.initElements(driver, AddNewPostPage.class);
		newPost.addNewPost(title, description);
	}
	
	public void editAPost(String presentTitle, String newTitle, String description) {
		Actions builder = new Actions(driver);
		builder.moveToElement(posts).build().perform();
		
		wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='menu-posts']//a[contains(@href,'edit.php')]")));
		allPosts = driver.findElement(By.xpath("//*[@id='menu-posts']//a[contains(@href,'edit.php')]"));
		allPosts.click();
		
		goToParticularPostPage(presentTitle);
		EditPostPage  editPost = PageFactory.initElements(driver, EditPostPage.class);
		editPost.editPost(newTitle, description);
	}
	
	public void deleteAPost (String title) {
		Actions builder = new Actions(driver);
		builder.moveToElement(posts).build().perform();
		
		wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='menu-posts']//a[contains(@href,'edit.php')]")));
		allPosts = driver.findElement(By.xpath("//*[@id='menu-posts']//a[contains(@href,'edit.php')]"));
		allPosts.click();
		
		goToParticularPostPage(title);
		DeletePostPage deletePost = PageFactory.initElements(driver, DeletePostPage.class);
		deletePost.delete();
	}
	
	public void filterPostsByCategory(String category){
	//TODO
	}
	
	public void searchInPosts(String searchText){
	//TODO
	}
	
	
	public int getAllPostsCount(){
		Actions builder = new Actions(driver);
		builder.moveToElement(posts).build().perform();
		
		wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='menu-posts']//a[contains(@href,'edit.php')]")));
		allPosts = driver.findElement(By.xpath("//*[@id='menu-posts']//a[contains(@href,'edit.php')]"));
		allPosts.click();
		
		List<WebElement> postsList = postsContainer.findElements(By.tagName("tr"));
		
		return postsList.size();
	}
	

	private void goToParticularPostPage(String title) {
		List<WebElement> allPosts = postsContainer.findElements(By.className("row-title"));
		
		for(WebElement ele : allPosts){
			if(ele.getText().equals(title)){
				Actions builder = new Actions(driver);
				builder.moveToElement(ele);
				builder.click(driver.findElement(By.cssSelector(".edit>a")));
				Action compositeAction = builder.build();
				compositeAction.perform();
				break;
			}
		}
	}
}
